package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("增加图书");
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入书名");
        String name = scan.nextLine();

        int num = bookList.getBookNum();
        for(int i =0; i<num; ++i) {
            Book book = bookList.getBook(i);
            if(name.equals(book.getName())) {
                System.out.println("此书已存在，添加失败");
                return;
            }
        }

        System.out.println("请输入作者");
        String author = scan.nextLine();
        System.out.println("请输入价格");
        Double price = scan.nextDouble();
        scan.nextLine(); //清空缓冲区
        System.out.println("请输入类型");
        String type = scan.nextLine();

        //创建一本书对象
        Book book = new Book(name,author,price,type);
        int currentSize = bookList.getBookNum();
        int maxSze = bookList.getMaxNum();
        //判断空间是否已满
        if(currentSize < maxSze) {
            bookList.setBook(book,currentSize);
            bookList.setBookNum(++currentSize);
            System.out.println("添加成功！");
        }
        else{
            System.out.println("书架已满，无法添加");
        }

    }
}
